package cn.akaba.yun.appCls;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AppStringTool {
    public static String format(String tpl, Map<String, Object> params) {
        StringBuilder str = new StringBuilder();
        Matcher matcher = Pattern.compile("\\$\\{(\\w+)\\}").matcher(tpl);
        while(matcher.find()) {
            String paramWithBracket = matcher.group(0);
            String param = matcher.group(1);
            Object val = params.get(param);
            matcher.appendReplacement(str, val == null ? paramWithBracket : val.toString());
        }
        matcher.appendTail(str);
        return str.toString();
    }

    public static String urlEncode(String str) {
        return URLEncoder.encode(str, StandardCharsets.UTF_8);
    }

    public static String base64Decode(String str) {
        byte[] strList = Base64.getDecoder().decode(str);
        return Arrays.toString(strList);
    }
}
